package com.timothy.android.view;

import java.util.List;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.SurfaceHolder;
import com.timothy.android.util.NumberUtil;
import com.timothy.answer.ChoiceResult;
import com.timothy.chart.CanvasSetting;
import com.timothy.config.Configuration;
import com.timothy.statistics.StatisticsBuilder;

public class CylinderHorView extends BaseChartView{
	private final static String LOG_TAG = "Bar2DHonView";
	
	private final static float BAR_RATIO = 0.8f;
	
	protected GestureDetector mGestureDetector = null;
	
	public CylinderHorView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void drawChart(SurfaceHolder sfh,BaseBean baseBean,Integer page,Integer screenSize ){
		Canvas canvas = sfh.lockCanvas();// 获得画布
		
		int canvasHeight = canvas.getHeight();
		int canvasWidth = canvas.getWidth();
		Log.i(LOG_TAG, "Canvas Height:"+canvasHeight);
		Log.i(LOG_TAG, "Canvas Width:"+canvasWidth);
		
		CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,screenSize);
		
		int leftM = setting.getLeftMargin();
		int rightM = setting.getRightMargin();
		int topM = setting.getTopMargin();
		int bottomM = setting.getBottomMargin();
		
		Log.i(LOG_TAG, "Canvas:leftM:"+leftM);
		Log.i(LOG_TAG, "Canvas:rightM:"+rightM);
		Log.i(LOG_TAG, "Canvas:topM:"+topM);
		Log.i(LOG_TAG, "Canvas:bottomM:"+bottomM);
		
        canvas.drawColor(setting.getBgColor());
		
		//write question
		Paint mPaint = new Paint();
		mPaint.setColor(Color.parseColor(CanvasSetting.CHART_QUESTION_TEXT_COLOR));		
		mPaint.setTextSize(setting.getQueTextSize());
		int queX = setting.getLeftMargin();
		int queY = setting.getTopMargin();
		Log.i(LOG_TAG, "queX:"+queX);
		Log.i(LOG_TAG, "queY:"+queY);
		
		String questionString = baseBean.getQuestionText();
/*		if("42".equals(type)&&priority!=null){//priortiy
			questionString = questionString + "(priority = "+priority+")";
		}*/
		
		int queLength = questionString.length();
		Log.i(LOG_TAG, "queLength:"+queLength);
		
		int line = 0;
		int lineHeight = 0;
		if(queLength>CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL){
			Log.i(LOG_TAG, "draw question text->queLength>"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL);
			for(line = 0;line<queLength/CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL;line++){
				canvas.drawText(questionString.substring(CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line,CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1)),queX,queY/4+lineHeight,mPaint);
				Log.i(LOG_TAG, "start:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line+";end:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*(line+1));
				lineHeight+= queY/4;
			}
			Log.i(LOG_TAG, "start:"+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL*line);
			canvas.drawText(questionString.substring(line*CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL),queX,queY/2+lineHeight,mPaint);
		}else{
			Log.i(LOG_TAG, "draw question text->queLength<="+CanvasSetting.QUESTION_WRAP_LENGTH_HORIZONTAL);
			canvas.drawText(questionString,queX,queY/2,mPaint);
		}
		
		canvas.save();
		
		
		//---------------------draw bar list start-------------------
	
		int canvasNoMarginHeight = canvasHeight - setting.getTopMargin() - setting.getBottomMargin();
		double barHeightInclSpace = (canvasNoMarginHeight * 1.0)/baseBean.getTotalCount();
		
		if(baseBean.getTotalPages()>1){
			barHeightInclSpace = (canvasNoMarginHeight * 1.0)/StatisticsBuilder.CHOICE_COUNT_PER_PAGE;
		}
		
		int barHeight = (int)(barHeightInclSpace*0.9);
		int spaceHeight = (int)(barHeightInclSpace*0.1);
		if(spaceHeight<1) spaceHeight = 1;
		
		int canvasNoMarginWidth = canvasWidth-setting.getLeftMargin()-setting.getRightMargin();
		
		PageBaseBean pageBean = baseBean.getCurrentPageBaseBean(page);
		List<ChoiceResult> pageList = pageBean.getCrList();
		int pageListSize = pageList.size();
		if(pageListSize==0) return;
		
		for(int i=0;i<pageListSize;i++){
			ChoiceResult cr = pageList.get(i);
			
			int votes = cr.getCount();
			double votesShare = cr.getShare();
			int barWidth = (int) (votesShare * canvasNoMarginWidth * BAR_RATIO);
			
			Log.i(LOG_TAG, "barWidth:"+barWidth);
			Log.i(LOG_TAG, "votes:"+votes);
			Log.i(LOG_TAG, "votesShare:"+votesShare);
			
			int tlX = setting.getLeftMargin();
			int tlY = setting.getTopMargin()+ (int)(i*(barHeight + spaceHeight));
			int brX = tlX + barWidth;
			int brY = tlY + barHeight;
			Log.i(LOG_TAG, "tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
			
//			//draw bar
//			mPaint.setColor(Color.parseColor(Configuration.CHART_COLOR_ARR[i]));
//			canvas.drawRect(tlX,tlY,brX,brY, mPaint);
//			canvas.save();
			
			//draw cylinder
			int emboss = (int)(barHeight * 0.2f);
			
			//draw cylinder
			mPaint.setColor(Color.parseColor(Configuration.CHART_COLOR_ARR[i]));//圆柱底部颜色
			canvas.drawOval(new RectF(brX-emboss,tlY,brX+emboss,brY), mPaint);//cylinder bottom
			mPaint.setColor(Color.parseColor(Configuration.CHART_COLOR_ARR[i]));//圆柱中间颜色
			canvas.drawRect(tlX,tlY,brX,brY, mPaint);
			mPaint.setColor(Color.parseColor(Configuration.SHADOW_COLOR_ARR[i]));//圆柱顶部颜色
			canvas.drawOval(new RectF(tlX-emboss,tlY,tlX+emboss,brY), mPaint);//cylinder top			
			
			//draw choice text
			mPaint.setTextSize(setting.getAnswerTextSize());
			mPaint.setColor(Color.parseColor(Configuration.CHART_CHOICE_TEXT_COLOR));
			canvas.drawText(String.valueOf(cr.getIndex()),leftM/2, tlY+barHeight/2, mPaint);
			
			
			//draw answer text
			mPaint.setTextSize(setting.getChoiceTextSize());
			mPaint.setColor(Color.parseColor(Configuration.CHART_ANSWER_TEXT_COLOR));
			//answerTextStyle = 1 show votes;
			//answerTextStyle = 2 show share;
			//answerTextStyle = 3 show votes and share;
			String votesStr = String.valueOf(votes);
			String shareStr = "0%";
			if(votesShare*100>0){
				shareStr = NumberUtil.formatData0Deci(votesShare*100)+"%";
			}
			int answerTextStyle = baseBean.getAnswerTextStyle();
			switch (answerTextStyle){
			case 1:canvas.drawText(votesStr, tlX + barWidth+leftM/2, tlY+barHeight/2, mPaint);break;
			case 2:canvas.drawText(shareStr, tlX + barWidth+leftM/2+leftM, tlY+barHeight/2, mPaint);break;
			case 3:
				canvas.drawText(votesStr, tlX + barWidth+leftM/2, tlY+barHeight/2, mPaint);
				canvas.drawText(shareStr, tlX + barWidth+leftM/2+leftM, tlY+barHeight/2, mPaint);
			}
			canvas.save();
			

			canvas.save();
		}
		
//		//draw page bullets list at canvas left
//		if(baseBean.getTotalPages()>1){
//			int pageY = setting.getTopMargin();
//			for(int p=0;p<baseBean.getTotalPages();p++){
//				mPaint.setColor(Color.GRAY);
//				if(p+1 == page){
//					mPaint.setColor(Color.WHITE);
//				}
//				canvas.drawCircle(setting.getLeftMargin()/4, pageY, setting.getBulletRadius(), mPaint);
//				pageY+=setting.getBulletRadius()*3;
//			}
//			canvas.save();
//		}
		
		//draw page bullets list at canvas bottom
		if(baseBean.getTotalPages()>1){
			int pageX = setting.getLeftMargin();
			for(int p=0;p<baseBean.getTotalPages();p++){
				mPaint.setColor(Color.GRAY);
				if(p+1 == page){
					mPaint.setColor(Color.WHITE);
				}
				canvas.drawCircle(pageX, canvasHeight-setting.getBottomMargin()/4, setting.getBulletRadius(), mPaint);
				pageX+=setting.getBulletRadius()*3;
			}
			canvas.save();
		}

		canvas.restore();
		sfh.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
	}

	
}
